package cn.net.wangyixinwen.mvvm.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.net.wangyixinwen.bean.OrderBean;
import cn.net.wangyixinwen.databinding.FragmentTypeBinding;
import cn.net.wangyixinwen.mvvm.activity.NewsDetailsActivity;
import cn.net.wangyixinwen.mvvm.adapter.JXAdapter;
import cn.net.wangyixinwen.mvvm.adapter.OrderAdapter;
import cn.net.wangyixinwen.mvvm.adapter.TypeAdapter;
import cn.net.wangyixinwen.mvvm.vm.TypeVM;
import cn.net.wangyixinwen.test.OrderModule;

public class TypeFragment extends Fragment {

    private FragmentTypeBinding viewBinding;

    private TypeVM vm = new TypeVM();

    private TypeAdapter typeAdapter = new TypeAdapter();

    private JXAdapter jxAdapter =  new JXAdapter();



    private List<OrderBean.ListDTO> olist ;
    private OrderAdapter orderAdapter ;


    private ViewModelProvider provider ;
    private OrderModule module ;
    private MutableLiveData<OrderBean> liveData ;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewBinding = FragmentTypeBinding.inflate(LayoutInflater.from(requireContext()));
        return viewBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initObserver();
        viewBinding.rvList1.setAdapter(typeAdapter);
        GridLayoutManager gl = new GridLayoutManager(requireContext(), 2);
        viewBinding.rvList2.setAdapter(jxAdapter);
        viewBinding.rvList2.setLayoutManager(gl);




        typeAdapter.setOnItemClickListener((adapter, view1, position) -> {
            for (int i = 0; i < typeAdapter.getData().size(); i++) {
                typeAdapter.getData().get(i).setSelect(position == i);
                if (position == typeAdapter.getData().size()-1){
                    getOrderData();
                }else {
                    vm.getTypeList(typeAdapter.getData().get(position));
                    viewBinding.rvList2.setAdapter(jxAdapter);
                }
            }

            typeAdapter.notifyDataSetChanged();

        });

        jxAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                Intent intent = new Intent(requireContext(), NewsDetailsActivity.class);
                intent.putExtra("data", jxAdapter.getItem(position));
                startActivity(intent);
            }
        });


        vm.getFenLei();
    }

    public void initObserver() {
        vm.eventList.observe(this, list -> {
            typeAdapter.setNewData(list);
        });

        vm.newListEvent.observe(this, list -> {
            jxAdapter.setNewData(list);
        });



    }


    private void getOrderData(){
        olist = new ArrayList<>() ;
        viewBinding.rvList2.setLayoutManager(new GridLayoutManager(getActivity(),2));

        provider = new ViewModelProvider(this) ;
        module = provider.get(OrderModule.class) ;
        module.setModel();
        liveData = (MutableLiveData<OrderBean>) module.getLiveData() ;
        liveData.observe(getActivity(), new Observer<OrderBean>() {
            @Override
            public void onChanged(OrderBean orderBean) {
                olist = orderBean.getList() ;
                orderAdapter.updateAdapter(olist);
            }
        });

        module.getData();
        orderAdapter = new OrderAdapter(olist,getActivity()) ;
        viewBinding.rvList2.setAdapter(orderAdapter);
    }
}
